<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Brands_model extends CI_Model {
	function index($args){
		return $this->show($args);
	}
	function show($args) {
		$data['url'] = $this->url = $this->uri->segment(1);
		if ($args[0] != "") {
			$id = $args[0];
			$data['page'] = $page = $this->db->select()->from('catalog_brands')->where('brand_id', $id)->limit(1)->get()->row_array();
			$items = $this->db->select('item_parent')->from('catalog_items')->where('item_brand', $id)->where('item_status', 1)->order_by('item_order', "asc")->group_by('item_parent')->get()->result_array();
			$parents = array();
			foreach($items as $item){
				//$parents[] = "<a href='/catalog/'>Каталог</a>".$this->get_parents($item['item_parent']);
				$parents[] = $this->get_parents($item['item_parent']);
			}
			$data['items'] = $parents;
			$out['data']['crumps'] = " &raquo; <a href='/".$this->url."/".$page['brand_id']."'>".$page['brand_title']."</a>";
			$out['text'] = $this->load->view('modules/catalog/brand', $data, true);
			return $out;  
		} else {
			$data['pages'] = $pages = $this->db->select()->from('catalog_brands')->get()->result_array();
			$out['text'] = $this->load->view('modules/catalog/brands', $data, true);
			return $out; 
		}
	}
	private function get_parents($id = FALSE){
		if(!$id){
			return "";
		}
		$parent = $this->db->select('parent_id, parent_title, parent_parent')->from('catalog_parents')->where('parent_id',$id)->limit(1)->get()->row_array();
		if(sizeof($parent) > 0){
			$title = $this->get_parents($parent['parent_parent']);
			$title .= ($parent['parent_parent'] != 0?" > ":"")."<a href='/catalog/category/".$parent['parent_id']."'>".$parent['parent_title']."</a>";
			return $title;
		} else {
			return "";
		}
	}
}